package com.manager.plat.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.manager.plat.dao.IConfigBusinessDao;
import com.manager.plat.dao.IListBlackDao;
import com.manager.plat.vo.ConfigBusinessVo;
import com.manager.plat.vo.ListBlackVo;
import com.manager.plat.vo.UserInfoVo;
import com.opensymphony.xwork2.ActionSupport;

public class ListBlackAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = -7352540784823364793L;
	private static Logger logger = Logger.getLogger(ListBlackAction.class);
	
	private IListBlackDao listBlackDao;			//黑名单数据实现
	private IConfigBusinessDao configBusinessDao;//业务配置数据接口层
	private List<ListBlackVo> listBlackVoList;	//黑名单数据列表
	private List<ConfigBusinessVo> configBusinessVoList;//业务配置信息列表
	private ListBlackVo listBlackVo;			//黑名单数据信息VO
	private String actionPath;					//操作成功跳转路径 
	
	public String  getListBlackList(){
		listBlackVoList = listBlackDao.getListBlackList();
		return SUCCESS;
	}
	
	public String addListBlack(){
		listBlackVo = null;
		configBusinessVoList = configBusinessDao.getConfigBusinessList();
		return SUCCESS;
	}
	
	public String editListBlack(){
		if(listBlackVo.getId() != 0){
			listBlackVo = listBlackDao.getListBlackVoById(listBlackVo.getId());
		}
		configBusinessVoList = configBusinessDao.getConfigBusinessList();
		return SUCCESS;
	}
	
	public String updateListBlack(){
		HttpServletRequest request = ServletActionContext.getRequest();
		UserInfoVo platUser = (UserInfoVo) request.getAttribute("user");
		if(listBlackVo.getId() == 0){
			if("0".equals(listBlackVo.getSp_code())){
				List<ConfigBusinessVo> configBusinessList = configBusinessDao.getConfigBusinessList();
				if(configBusinessList != null && configBusinessList.size() > 0){
					List<ListBlackVo> listBlackList = new ArrayList<ListBlackVo>();
					String[] mobiles = listBlackVo.getMobile().split(":");
					for(ConfigBusinessVo configBusinessVo : configBusinessList){
						if(mobiles != null && mobiles.length > 0){
							for(String mobile : mobiles){
								ListBlackVo listBlack = new ListBlackVo();
								listBlack.setMobile(mobile);
								listBlack.setSp_code(configBusinessVo.getSp_code());
								
								listBlackList.add(listBlack);
							}
						}
					}
					
					if(listBlackList != null && listBlackList.size() > 0){
						int count = listBlackDao.batchInsertListBlack(listBlackList);
						
						if(count > 0){
							logger.info(String.format("[%s]添加[%s]黑名单信息成功", platUser.getLogin_username(),listBlackVo.getMobile()));
						}else{
							logger.info(String.format("[%s]添加[%s]黑名单信息失败", platUser.getLogin_username(),listBlackVo.getMobile()));
						}
					}
				}
			}else{
				String[] mobiles = listBlackVo.getMobile().split(":");
				if(mobiles != null && mobiles.length > 0){
					List<ListBlackVo> listBlackList = new ArrayList<ListBlackVo>();
					for(String mobile : mobiles){
						ListBlackVo listBlack = new ListBlackVo();
						listBlack.setMobile(mobile);
						listBlack.setSp_code(listBlackVo.getSp_code());
						
						listBlackList.add(listBlack);
					}
					
					if(listBlackList != null && listBlackList.size() > 0){
						int count = listBlackDao.batchInsertListBlack(listBlackList);
						
						if(count > 0){
							logger.info(String.format("[%s]添加[%s]黑名单信息成功", platUser.getLogin_username(),listBlackVo.getMobile()));
						}else{
							logger.info(String.format("[%s]添加[%s]黑名单信息失败", platUser.getLogin_username(),listBlackVo.getMobile()));
						}
					}
				}
			}
			
		}else {
			int count = listBlackDao.updateListBlack(listBlackVo);
			if(count > 0){
				logger.info(String.format("[%s]更新[%s]黑名单信息成功", platUser.getLogin_username(),listBlackVo.getMobile()));
			}else{
				logger.info(String.format("[%s]更新[%s]黑名单信息失败", platUser.getLogin_username(),listBlackVo.getMobile()));
			}
		}
		
		actionPath = "listBlack/getListBlackList.action";
		return SUCCESS;
	}
	
	public String deleteListBlack(){
		HttpServletRequest request = ServletActionContext.getRequest();
		UserInfoVo platUser = (UserInfoVo) request.getAttribute("user");
		
		int count = listBlackDao.deleteListBlack(listBlackVo.getId());
		if(count > 0){
			logger.info(String.format("[%s]删除[%s]黑名单信息成功", platUser.getLogin_username(),listBlackVo.getMobile()));
		}else{
			logger.info(String.format("[%s]删除[%s]黑名单信息失败", platUser.getLogin_username(),listBlackVo.getMobile()));
		}
		actionPath = "listBlack/getListBlackList.action";
		return SUCCESS;
	}
	
	public IListBlackDao getListBlackDao() {
		return listBlackDao;
	}
	public void setListBlackDao(IListBlackDao listBlackDao) {
		this.listBlackDao = listBlackDao;
	}
	public IConfigBusinessDao getConfigBusinessDao() {
		return configBusinessDao;
	}

	public void setConfigBusinessDao(IConfigBusinessDao configBusinessDao) {
		this.configBusinessDao = configBusinessDao;
	}

	public List<ListBlackVo> getListBlackVoList() {
		return listBlackVoList;
	}
	public void setListBlackVoList(List<ListBlackVo> listBlackVoList) {
		this.listBlackVoList = listBlackVoList;
	}
	public List<ConfigBusinessVo> getConfigBusinessVoList() {
		return configBusinessVoList;
	}

	public void setConfigBusinessVoList(List<ConfigBusinessVo> configBusinessVoList) {
		this.configBusinessVoList = configBusinessVoList;
	}

	public ListBlackVo getListBlackVo() {
		return listBlackVo;
	}
	public void setListBlackVo(ListBlackVo listBlackVo) {
		this.listBlackVo = listBlackVo;
	}
	public String getActionPath() {
		return actionPath;
	}
	public void setActionPath(String actionPath) {
		this.actionPath = actionPath;
	}
}
